home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / nn.zip / EXECUTE.C < prev    next >
C/C++ Source or Header  |  1989-12-31  |  3KB  |  159 lines

  1. #include <signal.h>
  2. #include <errno.h>
  3. #include "config.h"
  4. #include "term.h"
  5.  
  6. export int shell_restrictions = 0;    /* disable shell escapes */
  7.  
  8. char *user_shell;
  9.  
  10. static shell_check()
  11. {
  12.     if (shell_restrictions) {
  13.     msg("Restricted operation - not allowed");
  14.     return -1;
  15.     }
  16.     return 0;
  17. }
  18.  
  19.  
  20. init_execute()
  21. {
  22.     if ((user_shell = getenv("SHELL")) == NULL)
  23.     user_shell = SHELL;
  24. }
  25.  
  26. execute(path, args)
  27. char *path, **args;
  28. {
  29.     int was_raw, pid, i, status;
  30.     sig_type  (*quit)(), (*intr)(), (*tstp)();
  31.     extern int errno;
  32.     
  33.     was_raw = unset_raw();
  34.     
  35.     while ((pid = fork()) == -1) sleep(1);
  36.     
  37.     if (pid == 0) {
  38.     for (i = 3 ; i < 20 ; i++)
  39.         close(i);
  40.     
  41.     execv(path, args);
  42.     
  43.     fprintf(stderr, "%s: not found\n", path);
  44.     nn_exit(20);
  45.     }
  46.     quit = signal(SIGQUIT, SIG_IGN);
  47.     intr = signal(SIGINT,  SIG_IGN);
  48. #ifdef HAVE_JOBCONTROL
  49.     tstp = signal(SIGTSTP, SIG_DFL);
  50. #endif
  51.     while ((i = wait(&status)) != pid && (i != -1 || errno == EINTR));
  52.     
  53.     signal(SIGQUIT, quit);
  54.     signal(SIGINT,  intr);
  55. #ifdef HAVE_JOBCONTROL
  56.     signal(SIGTSTP, tstp);
  57. #endif
  58.     
  59.     if (was_raw) raw();
  60.     
  61.     return status != 0;
  62. }
  63.  
  64.  
  65. shell_escape()
  66. {
  67.     static char command[FILENAME] = "";
  68.     char *cmd;
  69.     int first = 1;
  70.  
  71.     if (shell_check()) return 0;
  72.  
  73.     prompt("!");
  74.     
  75. again:    
  76.     
  77.     cmd = get_s(command, NONE, NONE, NO_COMPLETION);
  78.     if (cmd == NULL) return !first;
  79.     
  80.     strcpy(command, cmd);
  81.     
  82.     if (!run_shell(command, first)) return !first;
  83.     first = 0;
  84.     
  85.     if (any_key(0) == '!') {        /* should use key map here */
  86.     putchar(CR);
  87.     putchar('!');
  88.     clrline();
  89.     goto again;
  90.     }
  91.     
  92.     return 1;
  93. }
  94.  
  95.  
  96. static char *exec_sh_args[] = {
  97.     "nnsh", 
  98.     "-c", 
  99.     (char *)NULL, /* cmdstring */
  100.     (char *)NULL 
  101. };
  102.     
  103. run_shell(command, clear)
  104. char *command;
  105. int clear;    /* -1 => no output, 0 => CR/NL, 1 => clear */
  106. {
  107.     char cmdstring[512];
  108.     
  109.     if (shell_check()) return 0;
  110.  
  111.     if (!expand_file_name(cmdstring, command))
  112.     return 0;
  113.     
  114.     if (clear > 0) {
  115.     clrdisp();
  116.     fl;
  117.     } else if (clear == 0) {
  118.     putchar(CR);
  119.     putchar(NL);
  120.     }
  121.  
  122.     exec_sh_args[2] = cmdstring;
  123.     
  124.     execute(user_shell, exec_sh_args);
  125.     return 1;
  126. }
  127.     
  128. #ifndef HAVE_JOBCONTROL
  129. static char *exec_suspend_args[] = {
  130.     "nnsh", 
  131.     "-i", 
  132.     (char *)NULL
  133. };
  134. #endif
  135.  
  136. suspend_nn()
  137. {
  138.     int was_raw;
  139.     
  140.     if (shell_check()) return 0;
  141.  
  142.     was_raw = unset_raw();
  143.     gotoxy(0, Lines-1);
  144.     clrline();
  145.     visual_off();
  146.     
  147. #ifdef HAVE_JOBCONTROL
  148.     kill(process_id, SIGSTOP);
  149. #else
  150.     execute(user_shell, exec_suspend_args);
  151. #endif
  152.  
  153.     visual_on();
  154.     s_redraw++;
  155.     if (was_raw) raw();
  156.     
  157.     return 1;
  158. }
  159.